package com.zh.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zh.entity.AuthTongdun;
import com.zh.entity.Business;
import com.zh.entity.QueryLog;
import com.zh.mapper.AuthTongdunMapper;
import com.zh.service.AuthTongdunService;
import com.zh.service.BusinessService;
import com.zh.service.QueryLogService;
import com.zh.util.demo.PreloanQueryResponse;
import com.zh.util.demo.PreloanSubmitResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * 同盾贷前审核数据 服务实现类
 * </p>
 *
 * @author wiki
 * @since 2018-03-22
 */
@Service
@Profile("dev")
public class AuthTongdunServiceImplDev extends ServiceImpl<AuthTongdunMapper, AuthTongdun> implements AuthTongdunService {


    @Autowired
    private BusinessService businessService;

    @Autowired
    private QueryLogService queryLogService;

    @Override
    public PreloanSubmitResponse getPreloanSubmitResponse(Map<String, Object> tdps) {
        PreloanSubmitResponse preloanSubmitResponse = new PreloanSubmitResponse();
        preloanSubmitResponse.setReport_id("ER20180303091735D6B4394B");
        preloanSubmitResponse.setSuccess(true);
        return preloanSubmitResponse;
    }

    @Override
    public PreloanQueryResponse getPreloanQueryResponse(String reportId) {

        AuthTongdun authTongdun = baseMapper.selectById(1);
        PreloanQueryResponse preloanQueryResponse = new PreloanQueryResponse();

        preloanQueryResponse.setAddress_detect(JSONObject.parseObject(authTongdun.getAddressDetect()));
        preloanQueryResponse.setApplication_id("");
        preloanQueryResponse.setApply_time(1L);
        preloanQueryResponse.setFinal_decision("Review");
        preloanQueryResponse.setReport_id(authTongdun.getReportId()+"test");
        preloanQueryResponse.setReport_time(1L);
        preloanQueryResponse.setSuccess(true);
        preloanQueryResponse.setFinal_score(49);
        preloanQueryResponse.setRisk_items(JSONArray.parseArray(authTongdun.getRiskItems()));

        return  preloanQueryResponse;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void authIdentity(AuthTongdun authTongdun, Business business, QueryLog queryLog) {
        queryLogService.insert(queryLog);
        businessService.updateById(business);
        baseMapper.insert(authTongdun);

    }
}
